#include "StdAfx.h"
#include "Utility.h"
#include "CustomHyperLink.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomHyperLink, CButton)

CCustomHyperLink::CCustomHyperLink(void)
{
	//String and Font Parameter
	m_strText=_T("");
	m_nFontSize = 10;
	m_nFontStyle = FontStyleRegular;
	m_enFontUnit = UnitPoint;
	m_strTextFormat = NULL;
	m_pFontFamily = NULL;
	m_pFontBrush = NULL;
	m_pFont = NULL;

	//Image and Handle Parameter
	m_hBgDC = m_hDrawDC = NULL;
	m_hPParentWnd = m_hParentWnd = NULL;

	m_nShow = SW_HIDE;
	m_hLinkCursor = NULL;
	m_bRunProcess = m_bRunURL = false;
	m_strURL = m_strProcess = m_strCmd = _T("");
}

CCustomHyperLink::~CCustomHyperLink(void)
{
	if(m_pFont != NULL){ delete m_pFont;  m_pFont = NULL; }
	if(m_pFontBrush != NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	if(m_pFontFamily != NULL){ delete m_pFontFamily;  m_pFontFamily = NULL; }
	if(m_strTextFormat != NULL){ delete m_strTextFormat;  m_strTextFormat = NULL; }	
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomHyperLink::SetURL(const CString &strURL)
{
	if(!strURL.IsEmpty())
	{
		m_bRunURL = true;
		m_bRunProcess = false;
		m_strURL = strURL;
	}
}

void CCustomHyperLink::SetProcess(const CString &strProcess, const CString &strCmd, int nshow)
{
	if(!strProcess.IsEmpty())
	{
		m_bRunProcess = true;
		m_bRunURL = false;
		m_strProcess = strProcess;
		m_strCmd = strCmd;
		m_nShow = nshow;
	}
}

void CCustomHyperLink::Initialize(HWND hPParentWnd, HWND hParentWnd, HDC hBgDC, HDC hDrawDC, int nParentx, int nParenty)
{
	m_hPParentWnd = hPParentWnd;
	m_hParentWnd = hParentWnd;

	m_hBgDC = hBgDC;
	m_hDrawDC = hDrawDC;

	::GetWindowRect(m_hPParentWnd, m_rtPPRect);
	::GetWindowRect(m_hParentWnd, m_rtPRect);

	GetWindowRect(&m_rtRect);
	m_rtRect.OffsetRect(+nParentx, +nParenty);
	m_rtRect.OffsetRect(-m_rtPRect.left, -m_rtPRect.top);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//String Font Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomHyperLink::SetWindowTextW(LPCTSTR lpszString)
{
	CButton::SetWindowTextW(lpszString);
	Invalidate();
}

void CCustomHyperLink::SetFontSize(int nSize)
{
	if(nSize > 0)
		m_nFontSize = nSize;
}

void CCustomHyperLink::SetFontStyle(UINT nStyle)
{
	if(nStyle >= 0)
		m_nFontStyle = nStyle;
}

void CCustomHyperLink::SetFontColor(int nR, int nG, int nB)
{
	if(m_pFontBrush!=NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	m_pFontBrush = new SolidBrush(Color(247, nR, nG, nB));	

}
void CCustomHyperLink::SetFontFamily(CString strFontFamilyName)
{
	if(!strFontFamilyName.IsEmpty())
	{
		if(m_pFontFamily!=NULL){ delete m_pFontFamily;  m_pFontFamily=NULL; }
		m_pFontFamily = new FontFamily(strFontFamilyName);
	}
}

void CCustomHyperLink::CreateFont()
{
	//----------------------new FontFamily
	if(m_pFontFamily == NULL)
		m_pFontFamily = new FontFamily(L"Arial");
	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus())
	{
		if (this->m_hWnd)
		{
			CFont *pCurFont = this->GetFont();  LOGFONT LgFong;  pCurFont->GetLogFont(&LgFong);
			if(m_pFontFamily){ delete m_pFontFamily;  m_pFontFamily = NULL;  m_pFontFamily = new FontFamily(LgFong.lfFaceName); }
		}
	}
	
	//----------------------new Font
	if(m_pFont == NULL)
		m_pFont = new Gdiplus::Font(m_pFontFamily,(Gdiplus::REAL)m_nFontSize, m_nFontStyle | FontStyleUnderline, m_enFontUnit);

	//----------------------new StringFormat
	if(m_strTextFormat == NULL)
	{
		m_strTextFormat = new StringFormat;
		m_strTextFormat->SetAlignment(StringAlignmentCenter);
		m_strTextFormat->SetLineAlignment(StringAlignmentCenter);
		m_strTextFormat->SetTrimming(StringTrimmingEllipsisCharacter);
	}

	//----------------------new FontBrush
	if (m_pFontBrush == NULL)
		m_pFontBrush = new SolidBrush(Color(247, 8, 8, 8));	
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomHyperLink::DrawItem(LPDRAWITEMSTRUCT di)
{
	DrawUI();
}

void CCustomHyperLink::DrawUI()
{
	BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);

	Graphics graphics( m_hDrawDC );

	GetWindowText(m_strText);
	if(!m_strText.IsEmpty())
	{
		CreateFont();
		graphics.DrawString(m_strText.GetBuffer(), -1, m_pFont, 
			                RectF((Gdiplus::REAL)m_rtRect.left,    (Gdiplus::REAL)m_rtRect.top,
			                      (Gdiplus::REAL)m_rtRect.Width(), (Gdiplus::REAL)m_rtRect.Height()),
			                m_strTextFormat, m_pFontBrush);
	}

	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomHyperLink, CButton)
	ON_WM_DRAWITEM()
	ON_WM_ERASEBKGND()
	ON_CONTROL_REFLECT(BN_CLICKED, &CCustomHyperLink::OnBnClicked)
	ON_WM_SETCURSOR()
END_MESSAGE_MAP()

void CCustomHyperLink::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	ModifyStyle(0, BS_OWNERDRAW);
	CButton::PreSubclassWindow();
}

BOOL CCustomHyperLink::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

BOOL CCustomHyperLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	if (m_hLinkCursor == NULL)
		 m_hLinkCursor=::LoadCursor(NULL, IDC_HAND);

	::SetCursor(m_hLinkCursor);
	return TRUE;
}

void CCustomHyperLink::OnBnClicked()
{
	if(m_bRunProcess)
		ShellExecute(NULL, _T("open"), m_strProcess, m_strCmd, NULL, m_nShow);
	else if(m_bRunURL)
		ShellExecute(NULL, _T("open"), m_strURL, NULL, NULL, m_nShow);
}


